import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart' ;
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mfkb/config/Config.dart';
import 'package:mfkb/plugin/MyBehavior.dart';
import 'package:mfkb/service/MarketService.dart';
import 'package:toast/toast.dart';
import 'package:tobias/tobias.dart';

class Orders extends StatefulWidget {
  dynamic def ;
  dynamic data ;
  Orders({Key key , @required this.def , @required this.data}):super(key:key) ; 
  @override
  _OrdersState createState() => _OrdersState();
}

class _OrdersState extends State<Orders> {

  bool canPay = false ;

  @override
  void initState() {
    super.initState();
    MarketService().payCheck(widget.data["orderId"]).then((value){
      if(value["code"] == 200){
        canPay = true ;
      }
    }) ;
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark ,
      child: Scaffold(
        body:Container(
          color: Color.fromARGB(255, 246, 246, 248),
          child: Column(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).padding.top ,
              ) ,
              Container(
                height: 45,
                child: Row(
                  children: <Widget>[
                    Container(
                      width: 17,
                      height: 17,
                      margin: EdgeInsets.only(left:ScreenUtil().setWidth(19)),
                      child: GestureDetector(
                        child: Container(
                          width: 17,
                          height: 17,
                          child: Image.asset("assets/images/back.png" , fit: BoxFit.fill,),
                        ),
                        onTap: (){
                          Navigator.of(context).pop() ;
                        },
                      ),
                    ) ,
                    Expanded(
                      child: Container(
                        child: Center(
                          child: Text("填写订单" , style: TextStyle(
                            color: Color.fromARGB(255, 19 , 28 , 53) ,
                            fontSize: ScreenUtil().setSp(18) ,
                            fontWeight: FontWeight.bold 
                          ),),
                        ),
                      ),
                    ) ,
                    Container(
                      height: 17,
                      width: 36,
                      margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                    )
                  ],
                ),
              ) ,
              Expanded(
                child: ScrollConfiguration(
                  behavior: MyBehavior() ,
                  child: ListView(
                    padding: EdgeInsets.all(0),
                    children: <Widget>[
                      Container(
                        height: ScreenUtil().setHeight(110),
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(10) , right:ScreenUtil().setWidth(12) , top:ScreenUtil().setHeight(10)),
                        decoration: BoxDecoration(
                          color: Colors.white ,
                          borderRadius: BorderRadius.all(Radius.circular(12))
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start ,
                          children: <Widget>[
                            Container(
                              height: ScreenUtil().setHeight(25),
                              margin: EdgeInsets.only(left:ScreenUtil().setWidth(19) , top:ScreenUtil().setHeight(12) , right:ScreenUtil().setWidth(12)),
                              child: Row(
                                children: <Widget>[
                                  Expanded(
                                    child: Container(
                                      child: Text(widget.def["detail"] , style: TextStyle(
                                        fontSize: ScreenUtil().setSp(18) ,
                                        color: Color.fromARGB(255, 19, 28, 53) ,
                                        fontWeight: FontWeight.bold ,
                                        height: ScreenUtil().setHeight(25) / ScreenUtil().setSp(18)
                                      ), overflow: TextOverflow.ellipsis,),
                                    ),
                                  ) ,
                                  GestureDetector(
                                    child: Container(
                                      height: ScreenUtil().setHeight(24),
                                      width: ScreenUtil().setWidth(46),
                                      decoration: BoxDecoration(
                                        border: Border.all(
                                          color:Color.fromARGB(255, 236, 237, 241) ,
                                        ) ,
                                        borderRadius: BorderRadius.all(Radius.circular(14))
                                      ),
                                      child: Center(
                                        child: Text("更改" , style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12) ,
                                          color: Color.fromARGB(255, 0, 167, 187)
                                        ),),
                                      ),
                                    ),
                                    onTap: (){

                                    },
                                  ) 
                                ],
                              ),
                            ) ,
                            Container(
                              height: ScreenUtil().setHeight(17),
                              margin: EdgeInsets.only(left:ScreenUtil().setWidth(19) , top:ScreenUtil().setHeight(4) , right:ScreenUtil().setWidth(12)),
                              child: Text(widget.def["pplace"] , style: TextStyle(
                                fontSize: ScreenUtil().setSp(12) ,
                                color: Color.fromARGB(255, 121, 132, 155) ,
                                height: ScreenUtil().setHeight(17) / ScreenUtil().setSp(12)
                              ),),
                            ) ,
                            Container(
                              height: ScreenUtil().setHeight(20),
                              margin: EdgeInsets.only(left:ScreenUtil().setWidth(19) , top:ScreenUtil().setHeight(12) , right:ScreenUtil().setWidth(12)),
                              child:Text(widget.def["pname"] + "     " + widget.def["pphone"] , style: TextStyle(
                                fontSize: ScreenUtil().setSp(14) ,
                                color: Color.fromARGB(255, 19, 28, 53) ,
                                height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(14)
                              ),),
                            )
                          ],
                        ),
                      ) ,
                      Container(
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(10) , right:ScreenUtil().setWidth(10)),
                        child: Column(
                          children: List.generate(widget.data["goodsList"].length , (index){
                            return Container(
                              height: ScreenUtil().setHeight(159),
                              margin: EdgeInsets.only(top:ScreenUtil().setHeight(10)),
                              decoration: BoxDecoration(
                                color: Colors.white ,
                                borderRadius: BorderRadius.all(Radius.circular(12)) ,
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start ,
                                children: <Widget>[
                                  Container(
                                    height: ScreenUtil().setHeight(80),
                                    margin: EdgeInsets.only(left:ScreenUtil().setWidth(14) , top:ScreenUtil().setHeight(14) , right:ScreenUtil().setWidth(16)),
                                    child: Row(
                                      children: <Widget>[
                                        Container(
                                          width: ScreenUtil().setHeight(80),
                                          height: ScreenUtil().setHeight(80),
                                          decoration: BoxDecoration(
                                            border: Border.all(
                                              color:Color.fromARGB(255, 236, 237, 241)
                                            ) ,
                                            borderRadius: BorderRadius.all(Radius.circular(4))
                                          ),
                                          child: CachedNetworkImage(
                                            imageUrl: Config().getImgHost() + widget.data["goodsList"][index]["img"],
                                            fit: BoxFit.fill,
                                            placeholder: (contex , url){
                                              return Container(
                                                color:Color.fromARGB(255, 246, 245, 248) ,
                                                child: Center(
                                                  child: Image.asset("assets/images/imageloading.png"),
                                                ) ,
                                              ) ;
                                            }
                                          ),
                                        ) ,
                                        Expanded(
                                          child: Container(
                                            height: ScreenUtil().setHeight(80) ,
                                            margin: EdgeInsets.only(left:ScreenUtil().setWidth(11) , right:ScreenUtil().setWidth(16)),
                                            child: Column(
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              children: <Widget>[
                                                Container(
                                                  margin: EdgeInsets.only(top:1),
                                                  child: Text(widget.data["goodsList"][index]["gname"] , maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(
                                                    color: Color.fromARGB(255, 78, 86, 109) ,
                                                    fontSize: ScreenUtil().setSp(14) ,
                                                    fontWeight: FontWeight.bold 
                                                  ),),
                                                ) ,
                                                Container(
                                                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(8)),
                                                  child: Text(widget.data["goodsList"][index]["sname"] , maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(
                                                    color: Color.fromARGB(255, 121, 132, 155) ,
                                                    fontSize: ScreenUtil().setSp(11) ,
                                                  ),),
                                                ) ,
                                                Container(
                                                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(5)),
                                                  child: Row(
                                                    mainAxisAlignment: MainAxisAlignment.start ,
                                                    crossAxisAlignment: CrossAxisAlignment.end ,
                                                    children: <Widget>[
                                                      Text("￥" , style: TextStyle(
                                                        fontSize: ScreenUtil().setSp(10) ,
                                                        color: Colors.red ,
                                                        fontWeight: FontWeight.bold ,
                                                        height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(10)
                                                      ),textAlign: TextAlign.start,) ,
                                                      Text(widget.data["goodsList"][index]["price"].toString() , style: TextStyle(
                                                        fontSize: ScreenUtil().setSp(16) ,
                                                        color: Colors.red  ,
                                                        fontWeight: FontWeight.bold ,
                                                      ),textAlign: TextAlign.start,) ,
                                                      // Expanded(
                                                      //   child:Container(
                                                      //     height: ScreenUtil().setHeight(22),
                                                      //     child: Row(
                                                      //       mainAxisAlignment: MainAxisAlignment.end ,
                                                      //       // crossAxisAlignment: CrossAxisAlignment.center,
                                                      //       children: <Widget>[
                                                      //         GestureDetector(
                                                      //           child: Container(
                                                      //             width: ScreenUtil().setHeight(22),
                                                      //             height: ScreenUtil().setHeight(22),
                                                      //             color: Colors.white ,
                                                      //             child: Center(
                                                      //               child: Container(
                                                      //                 width: ScreenUtil().setHeight(18),
                                                      //                 height: ScreenUtil().setHeight(18),
                                                      //                 decoration: BoxDecoration(
                                                      //                   color: Color.fromARGB(255, 248, 248, 249) , 
                                                      //                   borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setHeight(9)))
                                                      //                 ),
                                                      //                 child: Center(
                                                      //                   child: Text("-" , style: TextStyle(
                                                      //                     // color: _select[index]["count"] == 1 ? Color.fromARGB(255, 205, 207, 221) : Color.fromARGB(255, 19, 28, 53) ,
                                                      //                     fontWeight: FontWeight.bold 
                                                      //                   )),
                                                      //                 ),
                                                      //               ),
                                                      //             )
                                                      //           ) ,
                                                      //           onTap: (){
                                                      //             // if(_select[index]["count"] <= 1){
                                                      //             //   return ;
                                                      //             // }else{
                                                      //             //   _select[index]["count"]-- ;
                                                      //             //   sum() ;
                                                      //             //   setState(() {});
                                                      //             // }
                                                      //           },
                                                      //         ) ,
                                                      //         Container(
                                                      //           height: ScreenUtil().setHeight(22),
                                                      //           width: ScreenUtil().setWidth(20),
                                                      //           margin: EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(13)),
                                                      //           child: Center(
                                                      //             child: Text(
                                                      //               widget.data["goodsList"][index]["num"].toString()
                                                      //               // _select[index]["count"].toString() , style: TextStyle(
                                                      //               //   color: Color.fromARGB(255, 19, 28, 53) ,
                                                      //               //   fontSize: ScreenUtil().setSp(16) ,
                                                      //               //   fontWeight: FontWeight.bold 
                                                      //               // ),
                                                      //             ),
                                                      //           ),
                                                      //         ) ,
                                                      //         GestureDetector(
                                                      //           child: Container(
                                                      //             width: ScreenUtil().setHeight(22) ,
                                                      //             height: ScreenUtil().setHeight(22) ,
                                                      //             color: Colors.white ,
                                                      //             child: Center(
                                                      //               child: Container(
                                                      //                 width: ScreenUtil().setHeight(18),
                                                      //                 height: ScreenUtil().setHeight(18),
                                                      //                 decoration: BoxDecoration(
                                                      //                   color: Color.fromARGB(255, 248, 248, 249) , 
                                                      //                   borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setHeight(9)))
                                                      //                 ),
                                                      //                 child: Center(
                                                      //                   child: Text("+" , style: TextStyle(
                                                      //                     // color: _select[index]["count"] == _list[index]["stock"] ? Color.fromARGB(255, 205, 207, 221) : Color.fromARGB(255, 19, 28, 53) ,
                                                      //                     fontWeight: FontWeight.bold 
                                                      //                   ),),
                                                      //                 ),
                                                      //               ),
                                                      //             ) ,
                                                      //           ),
                                                      //           onTap: (){
                                                      //             // if(_select[index]["count"] >= _list[index]["stock"]){
                                                      //             //   return ;
                                                      //             // }else{
                                                      //             //   _select[index]["count"]++ ;
                                                      //             //   sum() ;
                                                      //             //   setState(() { });
                                                      //             // }
                                                      //           },
                                                      //         ),
                                                      //       ],
                                                      //     ),
                                                      //   )
                                                      // )
                                                    ],
                                                  ),
                                                ) ,
                                              ],
                                            ),
                                          ),
                                        )
                                      ],
                                    ),
                                  ) ,
                                  Container(
                                    height: ScreenUtil().setHeight(1),
                                    color: Color.fromARGB(255, 236, 237, 241),
                                    margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , right:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(14)),
                                  ) ,
                                  Expanded(
                                    child: Container(
                                      margin: EdgeInsets.only(left:ScreenUtil().setWidth(14)),
                                      child: Text("备注" , style: TextStyle(
                                        color: Color.fromARGB(255, 19, 28, 53) ,
                                        fontSize: ScreenUtil().setSp(14) ,
                                        height: ScreenUtil().setHeight(38) / ScreenUtil().setSp(14)
                                      ),),
                                    ),
                                  )
                                ],
                              ),
                            ) ;
                          }),
                        ),
                      ) ,
                      Container(
                        height: ScreenUtil().setHeight(50),
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(10) , right:ScreenUtil().setWidth(10) , top:ScreenUtil().setHeight(10)),
                        decoration: BoxDecoration(
                          color: Colors.white ,
                          borderRadius: BorderRadius.all(Radius.circular(12)) 
                        ),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                              child: Container(
                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(16)),
                                child: Text("运费" , style: TextStyle(
                                  color: Color.fromARGB(255, 19, 28, 53) ,
                                  fontSize: ScreenUtil().setSp(16)
                                ),),
                              ),
                            ) ,
                            Container(
                              width: ScreenUtil().setWidth(66),
                              margin: EdgeInsets.only(right:ScreenUtil().setWidth(5)),
                              child: Row(
                                children: <Widget>[
                                  Container(
                                    width: ScreenUtil().setWidth(43),
                                    child: Text("免运费" , style: TextStyle(
                                      fontSize: ScreenUtil().setSp(14) ,
                                      color: Color.fromARGB(255, 121, 132, 155)
                                    ),),
                                  ) ,
                                  Container(
                                    margin: EdgeInsets.only(left:ScreenUtil().setWidth(7)),
                                    width: ScreenUtil().setWidth(16) ,
                                    height: ScreenUtil().setWidth(16) ,
                                    child: Image.asset("assets/images/more_2x.png" , fit: BoxFit.fill,),
                                  )
                                ],
                              ),
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              ) ,
              Container(
                height: ScreenUtil().setHeight(60),
                color: Colors.white ,
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Container(),
                    ) ,
                    GestureDetector(
                      child: Container(
                        width: ScreenUtil().setWidth(100),
                        height: ScreenUtil().setHeight(40),
                        margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                        decoration: BoxDecoration(
                          color: Color.fromARGB(255, 0, 195, 206) ,
                          borderRadius: BorderRadius.all(Radius.circular(21))
                        ),
                        child: Center(
                          child: Text("立即支付" , style: TextStyle(
                            fontSize: ScreenUtil().setSp(14) ,
                            fontWeight: FontWeight.bold ,
                            color:Colors.white 
                          ),),
                        ),
                      ),
                      onTap: () async { 
                        if(canPay){
                          MarketService().payInfo(widget.data["orderId"]).then((value) async{
                            if(value["code"] == 200){
                              var payResult = await aliPay(value["data"]) ;
                              if(payResult["resultStatus"].toString() == "6001"){
                                MarketService().canclePay(widget.data["orderId"]).then((value){
                                  print(value) ;
                                }) ;
                              }
                            }else if(value["code"] == 500){
                              Toast.show(value["msg"], context) ;
                            }else if(value["code"] == 201){
                              
                            }
                          }) ;
                        }
                      },
                    )
                  ],
                ),
              )
            ]
          ),
        )
      ),
    );
  }
}